<?php
if (! defined ( 'BASEPATH' ))
	exit ( 'No direct script access allowed' );

class Codevalue extends CI_Controller {
	var $eData = '';
	function __construct() {
		parent::__construct ();
		$this->thor->loginstate ();
	}
	
	function index() {
		$this->load->view ( 'mang/codevalue',$this->eData );
	}
	
	function edit(){
		$id = $this->input->get('id');
		$this->eData['cid'] = $id;
		$this->eData['id'] = $id;
		$this->eData['name'] = $this->getLabel($id);
		$this->db->where('name',$id);
		$this->db->order_by('sort', 'ASC');
		$this->eData['codeList'] = $this->db->get('codevalue')->result_array (0);
		$this->load->view('mang/codevalueedit',$this->eData);
	}
	
	function action(){
		$eCID = $this->input->get('cid');
		$id = $this->input->get('id');
		
		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="eError">', '</div>');
		$this->form_validation->set_rules('name', '名称', 'trim|required|min_length[2]|max_length[200]|xss_clean');
		if ($this->form_validation->run() == TRUE){
			$this->db->select_max("code");
			$this->db->where("name",$eCID);
			$query = $this->db->get('codevalue')->row_array();
			$operData = array();
			$operData['value'] = $this->input->post('name');
			if(isset($id)&& $id != null && $id != ''){
				$operData['id'] = $id;
				$this->db->where('id',$id);
				$this->db->update('codevalue',$operData);
			}else{
				$operData['name'] = $eCID;
				$operData['code'] = $query['code']+1;
				$operData['sort'] = 1;
				$this->db->insert('codevalue',$operData);
			}
			redirect('mang/codevalue/edit?id='.$eCID);
		}else{
			if(isset($id)&& $id != null && $id != ''){
				$this->db->where('id',$id);
				$this->eData['editCode'] = $this->db->get('codevalue')->row_array();
			}
			
			$this->eData['eCID'] = $eCID;
			$this->eData['eID'] = $id;
			$this->eData['title'] = $this->getLabel($eCID);
			$this->load->view('mang/codevalue_action',$this->eData);
		}
		
		
		
	}
	
	function delete(){
		$eCID   = $this->input->get('cid');
		$eID    = (int)$this->input->get('id');
		if($eID == 0) $eCheckID = $this->input->post('checkID');
		else $eCheckID[] = $eID;
		
		$this->db->where_in('id', $eCheckID);
		$this->db->delete('codevalue');
		
		
		redirect('mang/codevalue/edit?id='.$eCID);
	//	echo 'mang/codevalue/edit?id='.$eCID;
	}
	
	function order(){
		$eCID   = $this->input->get('cid');
		$eCheckID = $this->input->post('checkID');
		$eOrderID = $this->input->post('orderID');

		foreach($eCheckID as $i => $item){
			$this->db->where('id', $item);
			$this->db->update('codevalue', array('sort' => $eOrderID[$i]));
		}
		
		redirect('mang/codevalue/edit?id='.$eCID);
	}
	
	function getLabel($value){
		if($value == 'shenghuo'){
			return '生活';
		}else if($value == 'fushi'){
			return '服饰';
		}else if($value == 'wenhua'){
			return '文化';
		}else if($value == 'xiuxian'){
			return '休闲';
		}else if($value == 'meishi'){
			return '美食';
		}else if($value == 'ycpp'){
			return '原创品牌';
		}else if($value == 'yishu'){
			return '艺术中心';
		}else if($value == 'ditie'){
			return '地铁小店';
		}else if($value == 'qstTip'){
			return '密码问题提示';
		}else if($value == 'tsqy'){
			return '特色区域';
		}
	}

}